﻿using drawing_print.configure;
using drawing_print.form.custom;
using drawing_print.util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;

namespace drawing_print
{
    public partial class Form1 : Form
    {
        private List<ImageControl> imageControls;
        private int index;
        private ToolTip tooltip = new ToolTip();

        public Form1()
        {
            InitializeComponent();
            imageControls = new List<ImageControl>();
            this.index = 0;
            

        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            

            // 设置分割容器的大小
            this.splitContainer1.Width = this.ClientSize.Width;
            this.splitContainer1.Height = this.ClientSize.Height;

            List<Control> controls = new List<Control>();
            // 设置按钮大小
            var panl2 = this.splitContainer1.Panel2;
            for (int i = 0; i < panl2.Controls.Count; i++)
            {                
                for (int j = 0; j < panl2.Controls.Count; j++)
                {
                    if(panl2.Controls[j].TabIndex == i)
                    {
                        controls.Add(panl2.Controls[j]);
                    }
                    
                }
            }

            

            for (int i = 0; i < controls.Count; i++)
            {
                //panl2.GetNextControl()
                var element = controls[i];
                element.Width = panl2.Width - 10;
                element.Height = element.Width - element.Width / 3;
                if(i > 0)
                {
                    var up = controls[i - 1];
                    element.Location = new Point(up.Location.X, up.Location.Y + up.Height + 3);
                }
            }
                

        }
        public void tool(String txt, IWin32Window window)
        {
            //tooltip.Hide();
            tooltip.Dispose();
            tooltip = new ToolTip();

            tooltip.Show(txt, window);
        }
        private List<ImageControl> initData()
        {
           var list = new List<ImageControl>();


            //imagePath.Add(@"E:\source_code\gitee\drawing-print\drawing-print\drawing-print\Resources\test\kuan.jpg");
            //imagePath.Add(@"E:\source_code\gitee\drawing-print\drawing-print\drawing-print\Resources\test\chang.jpg");
            List<Bitmap> imagePath = new List<Bitmap>();
            imagePath.AddRange(AppConfig.ImageList);
            foreach (Bitmap path in imagePath)
            {

                // 新建一个流程
                var procedure = Procedure.init(path);
               
                var image = procedure.Image;

                var panel_w = this.splitContainer1.Panel1.ClientSize.Width;
                var panel_h = this.splitContainer1.Panel1.ClientSize.Height;

                // 缩小比例
                double suo_xiao = 1;
                if (image.Width > panel_w || image.Height > panel_h)
                {
                    double suo_1  = (double)panel_w / image.Width;

                    double suo_2 = (double)panel_h / image.Height;

                    if (suo_1 > suo_2)
                    {
                        suo_xiao = suo_2;
                    }
                    else
                    {
                        suo_xiao = suo_1;
                    }

                }


                    // 设置自定义控件的大小
                double controlsWidth = image.Width * suo_xiao;

                double controlsHeight = image.Height * suo_xiao;


                // 设置自定义控件左上角的 X,Y
                int controlsX = 0;
                int controlsY = 0;
                if (controlsWidth < panel_w)
                {
                    controlsX = (panel_w - (int)controlsWidth) / 2;
                }
                if (controlsHeight < panel_h)
                {
                    controlsY = (panel_h - (int)controlsHeight) / 2;
                }

                // 新建自定义插件
                ImageControl imageControl = new ImageControl(procedure);
                imageControl.Width = (int)controlsWidth;
                imageControl.Height = (int)controlsHeight;
                imageControl.init_width = (int)controlsWidth;
                imageControl.init_height = (int)controlsHeight;
                imageControl.zoom = 1.0;
                imageControl.Location = new Point(controlsX, controlsY);

                list.Add(imageControl);
            }
            return list;
        }
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);

            var list = initData();
            if (list.Count == 0)
            {
                MessageBox.Show("初始化失败。");
                return;
            }
            // 使用第一个
            this.splitContainer1.Panel1.Controls.Add(list[0]);
            this.index = 0;
            this.imageControls.AddRange(list);
            this.textBox1.Text = this.index + 1 + " / " + this.imageControls.Count;
        }


        

        /// <summary>
        /// 放大
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Enlarge_Click(object sender, EventArgs e)
        {
            var control = this.imageControl();
            control.zoom += 0.1;

            this.imageControl().deflat();
        }
        /// <summary>
        /// 缩小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Narrow_Click(object sender, EventArgs e)
        {
            var control = this.imageControl();
            control.zoom -= 0.1;
            this.imageControl().deflat();
        }

        private void Fallback_Click(object sender, EventArgs e)
        {
            var last = this.imageControl().fallback();
            this.jiaodu.Text = last.Angle.ToString();
            this.duibidu.Text = last.Contrast.ToString();
        }
        private Boolean check(string text)
        {
            // string parameter = @"^([1-9]|([1-9][0-9])|([1-2][0-9][0-9])|([3][0-5][0-9])|([0]{1}))$|^[3][5][9]$";
            string parameter = @"^-?[1-9]\d*|0$";

            if (Regex.IsMatch(text, parameter))
            {
                int a = Int32.Parse(text);
                if (a <= 180 && a >= -180)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 调整角度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Angle_Click(object sender, EventArgs e)
        {
            if (!check(this.jiaodu.Text))
            {
                MessageBox.Show("请输入正确的数值！");
                return;
            }
            int zs = Int32.Parse(this.jiaodu.Text);
            zs++;
            this.jiaodu.Text = zs.ToString();
            this.imageControl().drawing("Angle", zs);
        }
        private void Button1_Click(object sender, EventArgs e)
        {
            if (!check(this.jiaodu.Text))
            {
                MessageBox.Show("请输入正确的数值！");
                return;
            }
            int zs = Int32.Parse(this.jiaodu.Text);
            zs--;
            this.jiaodu.Text = zs.ToString();
            this.imageControl().drawing("Angle", zs);

        }
        /// <summary>
        /// 调整对比度
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button2_Click(object sender, EventArgs e)
        {
            if (!check(this.jiaodu.Text))
            {
                MessageBox.Show("请输入正确的数值！");
                return;
            }
            // TODO 获取真实的值
            int zs = Int32.Parse(this.duibidu.Text);
            zs++;
            this.duibidu.Text = zs.ToString();
            this.imageControl().drawing("Contrast", zs);
        }
        private void Button6_Click(object sender, EventArgs e)
        {
            if (!check(this.jiaodu.Text))
            {
                MessageBox.Show("请输入正确的数值！");
                return;
            }
            // TODO 获取真实的值
            int zs = Int32.Parse(this.duibidu.Text);
            zs--;
            this.duibidu.Text = zs.ToString();
            this.imageControl().drawing("Contrast", zs);
        }


        /// <summary>
        /// 回显操作后的值
        /// </summary>
        private void echo()
        {
            var control = this.imageControl();
            // TODO 显示实际值
        }
        /// <summary>
        /// 获取当前操作的图片
        /// </summary>
        /// <returns></returns>
        private ImageControl imageControl()
        {
            return this.imageControls[this.index];
        }
        
        /// <summary>
        /// 剪切
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button3_Click(object sender, EventArgs e)
        {
            //if (this.button3.BackColor == Color.FromArgb(64,64,64))
            //{
            //    this.button3.BackColor = Color.FromArgb(0,0,0);
            //}
            //else
            //{
            //    this.button3.BackColor = Color.FromArgb(64, 64, 64);
                
            //}
            //this.button3.Invalidate();
            this.imageControl().coverImage();
        }
        /// <summary>
        /// 上一个图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button4_Click(object sender, EventArgs e)
        {
            if(this.index == 0)
            {
                MessageBox.Show("当前为第一个图片");
                return;
            }

            this.splitContainer1.Panel1.Controls.Clear();
            this.splitContainer1.Panel1.Controls.Add(this.imageControls[this.index - 1]);
            this.index -= 1;
            this.textBox1.Text =  this.index + 1 + " / " + this.imageControls.Count;
        }
        /// <summary>
        /// 下一个图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button5_Click(object sender, EventArgs e)
        {
            if (this.index == this.imageControls.Count - 1)
            {
                MessageBox.Show("当前为最后一个图片");
                return;
            }
            this.splitContainer1.Panel1.Controls.Clear();
            this.splitContainer1.Panel1.Controls.Add(this.imageControls[this.index + 1]);
            this.index += 1;
            this.textBox1.Text = this.index + 1 + " / " + this.imageControls.Count;
        }

        /// <summary>
        /// 打印图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Print_Click(object sender, EventArgs e)
        {
            //List<Bitmap> list = new List<Bitmap>();
            //foreach (var imagecontrol in this.imageControls)
            //{
            //    list.Add(imagecontrol.procedures.last().Image);
            //}
            //bool res = Pdf.images2pdf(list, AppConfig.EndPath);
            //if (!res)
            //{
            //    MessageBox.Show("文件打印失败。");
            //}

            //MessageBox.Show("文件打印成功。");

            foreach (var imagecontrol in this.imageControls)
            {
                imagecontrol.print();
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            AppConfig.isWork = false;
            foreach(Bitmap image in AppConfig.ImageList)
            {
                image.Dispose();

            }
            AppConfig.ImageList.Clear();
            this.imageControls.Clear();
            this.tooltip.Dispose();

            Console.WriteLine("--------关闭窗体---------");
        }
        // ------------鼠标悬浮事件----------------
        private void Fallback_MouseEnter(object sender, EventArgs e)
        {
            this.tool("回退", this.Fallback);
        }

        private void Angle_MouseEnter(object sender, EventArgs e)
        {
            this.tool("左旋", this.angle);
        }

        private void Button1_MouseEnter(object sender, EventArgs e)
        {
            this.tool("右旋", this.button1);
        }

        private void Button2_MouseEnter(object sender, EventArgs e)
        {
            this.tool("增加对比度", this.button2);
        }

        private void Button6_MouseEnter(object sender, EventArgs e)
        {
            this.tool("减少对比度", this.button6);
        }

        private void Button3_MouseEnter(object sender, EventArgs e)
        {
            this.tool("遮挡", this.button3);
        }
        private void Button7_MouseEnter(object sender, EventArgs e)
        {
            this.tool("遮挡设置", this.button7);
        }

        private void Enlarge_MouseEnter(object sender, EventArgs e)
        {
            this.tool("放大",this.enlarge);
        }

        private void Narrow_MouseEnter(object sender, EventArgs e)
        {
            this.tool("缩小", this.narrow);
        }

        private void Button7_Click(object sender, EventArgs e)
        {
            drawing_print.form.settings settings = new form.settings();
            settings.ShowDialog();
        }

        private void TextBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && this.textBox1.Text != null)
            {
                var text = this.textBox1.Text.Trim();
                if (!text.Contains("/"))
                {
                    MessageBox.Show("页数格式不正确。");
                    this.textBox1.Text = this.index + 1 + " / " + this.imageControls.Count;
                    return;
                }
                string[] arr = text.Split('/');
                if (arr.Length != 2)
                {
                    MessageBox.Show("页数格式不正确。");
                    this.textBox1.Text = this.index + 1 + " / " + this.imageControls.Count;
                    return;
                }

                try
                {
                    int a = Int32.Parse(arr[0]);
                    int b = Int32.Parse(arr[1]);

                    if (a <= 0 || a > this.imageControls.Count)
                    {
                        MessageBox.Show("当前不能小于等于0，大于所有页。");
                        this.textBox1.Text = this.index + 1 + " / " + this.imageControls.Count;
                        return;
                    }

                    if (b != this.imageControls.Count)
                    {
                        MessageBox.Show("总页数错误。");
                        this.textBox1.Text = this.index + 1 + " / " + this.imageControls.Count;
                        return;
                    }

                    this.splitContainer1.Panel1.Controls.Clear();
                    this.splitContainer1.Panel1.Controls.Add(this.imageControls[a - 1]);
                    this.index = a - 1;
                    this.textBox1.Text = this.index + 1 + " / " + this.imageControls.Count;


                }
                catch
                {
                    MessageBox.Show("页数格式不正确。");
                    this.textBox1.Text = this.index + 1 + " / " + this.imageControls.Count;
                    return;
                }
            }
               

        }
    }
}
